package com.huatai.model.dubbo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huatai.common.util.BeanUtil;
import com.huatai.model.entity.*;
import com.huatai.model.mapper.DictionaryMapper;
import com.huatai.model.mapper.UploadTaskMapper;
import com.huatai.model.service.DictionaryService;
import com.huatai.model.service.FileService;
import com.huatai.model.service.ModelService;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;
import java.util.stream.Collectors;

@DubboService
public class ModelDubboApiServiceImpl implements ModelDubboApi{

	@Autowired
	ModelService modelService;

	@Autowired
	FileService fileService;

	@Autowired
	DictionaryMapper dictionaryMapper;

	@Autowired
	DictionaryService dictionaryService;

	@Override
	public Model getModelById(Long id) {
		ModelEntity entity= modelService.getById(id);
		Model model=null;
		try {
			if( null != entity ){
				modelService.buildExInfo(entity);
				model = BeanUtil.copy(entity,Model.class);
				model.setDiffuseMap(entity.getDiffuseMapyl());
				model.setMirrorMap(entity.getMirrorMapyl());
				model.setNormalMap(entity.getNormalMapyl());
				model.setMasterMap(entity.getMasterMapyl());
				model.setSimpleMap(entity.getSimpleMapyl());
				model.setIndustryKeyName(dictionaryMapper.getNameByKey(model.getIndustryKey()));
			}
		}catch (Exception e){
		}
		return model;
	}

	@Override
	public IPage<Model> modelPage(ModelQuery query) {
		Map param = new HashMap();
		if(StringUtils.isNotBlank(query.getModelName())){
			param.put("modelName",query.getModelName());
		}
		if(null != query.getPageIndex()){
			param.put("pageIndex",query.getPageIndex());
		}
		if(null != query.getPageSize()){
			param.put("pageSize",query.getPageSize());
		}
		IPage page= modelService.pageByRequestMap(param);
		List list = new ArrayList();
		if(null != page && page.getRecords().size()>0){
			List<DictionaryEntity> dictList= dictionaryService.list();
			Map<String,String> map = dictList.stream().collect(Collectors.toMap(DictionaryEntity::getConfigKey,DictionaryEntity::getConfigName));

			page.getRecords().stream().forEach(entity -> {
				Model model = BeanUtil.copy(entity,Model.class);
				//model.setFbxFileyl(modelService.getFileAbsolutePath(model.getFbxFile()));
				model.setDiffuseMap(modelService.getFileAbsolutePath(model.getDiffuseMap()));
				model.setMirrorMap(modelService.getFileAbsolutePath(model.getMirrorMap()));
				model.setNormalMap(modelService.getFileAbsolutePath(model.getNormalMap()));
				model.setMasterMap(modelService.getFileAbsolutePath(model.getMasterMap()));
				model.setSimpleMap(modelService.getFileAbsolutePath(model.getSimpleMap()));
				model.setSelectStatus(Boolean.FALSE);
				model.setIndustryKeyName(map.get(model.getIndustryKey()));
				list.add(model);
			});
		}
		page.setRecords(list);

		return page;
	}

	@Override
	public Integer getModelFilePartNums(String fileUrl) {
		return fileService.getFilePartNums(fileUrl);
	}

	@Override
	public Long getModelFileTotalSize(String fileUrl) {
		return fileService.getFileSize(fileUrl);
	}

	@Override
	public void download(String fileUrl, int partIndex) {
		fileService.download(fileUrl,partIndex);
	}

	@Override
	public void download(String fileUrl) {
		fileService.download(fileUrl);
	}

	@Override
	public void download(String fileUrl, Long offset, Long length) {
		fileService.download(fileUrl,offset,length);
	}

	@Override
	public FileInfo getFileInfo(String fileUrl) {

		FileInfo fileInfo = null;
		try {
			String bucketName = fileService.getBucketName(fileUrl);
			Long size = fileService.getFileSize(fileUrl);
			fileInfo = new FileInfo();
			fileInfo.setFilePath(fileUrl);
			fileInfo.setBucketName(bucketName);
			fileInfo.setTotalSize(size);
		}catch (Exception e){}

		return fileInfo;
	}
}
